﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;


namespace SimpleControlLibrary.Common
{
    public class EnumSelectorProperty
    {
        public static readonly DependencyProperty BindingPathProperty =
            DependencyProperty.RegisterAttached("BindingPath", typeof(string), typeof(EnumSelectorProperty), new PropertyMetadata(null, OnBindingPathChanged));

        static void OnBindingPathChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)
        {
            var selector = d as ComboBox;
            string path = e.NewValue as string;
            Type enumType = GetEnumType(d);
            
            if (selector != null && enumType != null)
            {
                if (path != null)
                {
                    string[] source = enumType.GetEnumNames();
                    if (!string.IsNullOrEmpty(GetFilter(d)))
                    {
                        string[] filters = GetFilter(d).Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                        selector.ItemsSource = source.Where(o => !filters.Contains(o));
                    }
                    else
                    {
                        selector.ItemsSource = source;
                    }
                    Binding binding = new Binding(path) { Mode = BindingMode.TwoWay , UpdateSourceTrigger= UpdateSourceTrigger.PropertyChanged};
                    selector.SetBinding(Selector.SelectedItemProperty, binding);
                }
            }
        }

        public static string GetBindingPath(DependencyObject obj)
        {
            return (string)obj.GetValue(BindingPathProperty);
        }
        public static void SetBindingPath(DependencyObject obj, string value)
        {
            obj.SetValue(BindingPathProperty, value);
        }


        public static readonly DependencyProperty EnumTypeProperty =
             DependencyProperty.RegisterAttached("EnumType", typeof(Type), typeof(EnumSelectorProperty), new PropertyMetadata(null));
        public static Type GetEnumType(DependencyObject obj)
        {
            return (Type)obj.GetValue(EnumTypeProperty);
        }
        public static void SetEnumType(DependencyObject obj, Type value)
        {
            obj.SetValue(EnumTypeProperty, value);
        }

        public static readonly DependencyProperty FilterProperty =
           DependencyProperty.RegisterAttached("Filter", typeof(string), typeof(EnumSelectorProperty),
                                               new PropertyMetadata(null));
        public static string GetFilter(DependencyObject obj)
        {
            return (string)obj.GetValue(FilterProperty);
        }
        public static void SetFilter(DependencyObject obj, string value)
        {
            obj.SetValue(FilterProperty, value);
        }
    }
}
